import { dateUtil as dayjs } from '/@/utils/dateUtil';
import { BasicColumn } from '/@/components/Table';
let isClick = false;
let selectRow: Recordable[] = [];
//返回周几
export const getWeek = (date) => {
  const week = dayjs(date).day();
  switch (week) {
    case 1:
      return '一';
    case 2:
      return '二';
    case 3:
      return '三';
    case 4:
      return '四';
    case 5:
      return '五';
    case 6:
      return '六';
    case 0:
      return '日';
  }
};

//获取月所有天数和周几
export const getMonthDays = (date) => {
  if (!date) return 0;
  //获取当月天数
  return dayjs(date).daysInMonth();
};
let getSelectedValue = () => {};
//获取月份天数及表头
export const getColumns = (date, fn) => {
  getSelectedValue = fn;
  const days = getMonthDays(date);
  const day = dayjs(date).format('YYYY-MM');
  const columns: BasicColumn[] = [
    {
      title: '名称',
      dataIndex: 'realName',
      width: 80,
    },
  ];
  for (let i = 1; i <= days; i++) {
    const curDay = `${day}-${i < 10 ? '0' + i : i}`;
    const curWeek = getWeek(curDay);
    columns.push({
      title: i,
      dataIndex: i + '',
      width: 40,
      children: [
        {
          title: curWeek,
          dataIndex: i + '',
          width: 40,
          customCell: handleCustomCell,
        },
      ],
    });
  }
  return columns;
};

export const handleCustomCell = (record, index, column) => {
  const dayChild = record.days[column.key - 1];
  if (dayChild.isAdjusted) {
    record[column.key] = '○';
  } else {
    record[column.key] = dayChild.selected;
  }
  return {
    props: {
      // draggable: 'true'
    },
    style: {
      // cursor: 'pointer',
      backgroundColor: dayChild['bgColor'] ? dayChild['bgColor'] : '#fff',
    },
    // 事件
    onClick: (event) => {
      console.log('click', { event, record, index, column });
    }, // 点击行
    onDblclick: (event) => {
      console.log('dblclick', { event, record, index });
    },
    onContextmenu: (event) => {
      console.log('contextmenu', { event, record, index });
    },
    onMousedown: (event) => {
      const selected = getSelectedValue();
      dayChild.bgColor = selected['color'];
      dayChild.shiftName = selected['name'];
      dayChild.isAdjusted = false;
      if (selected['id'] == -5) {
        dayChild.selected = '';
        if (dayChild.newShiftId != -1) {
          dayChild.operate = 0;
        } else {
          dayChild.operate = -1;
        }
      }

      if (selected['id'] != -5 && dayChild.shiftId != selected['id']) {
        dayChild.selected = dayChild.selected === '*' ? '' : '*';
        dayChild.operate = dayChild.selected === '*' ? 1 : -1;
        dayChild.shiftId = selected['id'];
      }
      record[column.key] = dayChild.selected;
      isClick = true;
      selectRow = [record];
    },
    onMouseup: (event) => {
      console.log('mouseup', { event, record, index });
      isClick = false;
    },
    onMouseover: () => {},
    onMouseenter: () => {
      if (isClick) {
        const selected = getSelectedValue();
        dayChild.bgColor = selected['color'];
        dayChild.shiftName = selected['name'];
        dayChild.isAdjusted = false;
        if (selected['id'] == -5) {
          dayChild.selected = '';
          if (dayChild.newShiftId != -1) {
            dayChild.operate = 0;
          } else {
            dayChild.operate = -1;
          }
        }

        if (selected['id'] != -5 && dayChild.shiftId != selected['id']) {
          dayChild.selected = dayChild.selected === '*' ? '' : '*';
          dayChild.operate = dayChild.selected === '*' ? 1 : -1;
          dayChild.shiftId = selected['id'];
        }
        record[column.key] = dayChild.selected;
        selectRow.push(record);
      }
    }, // 鼠标移入行
    mouseleave: () => {
      // {/* console.log('mouseleave', { event, record, index }) */}
    },
  };
};

export const getSelectRow = () => {
  return selectRow;
};
